package com.example.zhangxin.myhuatuob2b.fragment.home.home;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.activity.GoodsDetailsActivity;
import com.example.zhangxin.myhuatuob2b.adapter.GoodsListViewAdapter;
import com.example.zhangxin.myhuatuob2b.bean.GoodsList;
import com.example.zhangxin.myhuatuob2b.utils.URL;
import com.example.zhangxin.myhuatuob2b.view.RefreshListView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsListFragment extends Fragment implements RefreshListView.onRefreshListener, AdapterView.OnItemClickListener {


    public int key;
    public String keyword;
    public ArrayList<GoodsList> data;
    private RefreshListView ref_listview;
    private GoodsListViewAdapter adapter;
    private int pageno = 1;

    public GoodsListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_goods_list, container, false);
        data = new ArrayList<>();
        initView(v);
        return v;
    }

    private void initView(View v) {
        ref_listview = (RefreshListView) v.findViewById(R.id.ref_listview);
        data.clear();
        adapter = new GoodsListViewAdapter(getActivity(),data);
        ref_listview.setAdapter(adapter);

        loadingGoodsListData();

        ref_listview.setOnRefreshListener(this);
        ref_listview.setOnItemClickListener(this);
    }

    public void loadingGoodsListData(){

        String url = URL.URL_GOODSLIST + "&curpage=" + pageno + "&page=" + URL.PAGESIZE;
        if(!TextUtils.isEmpty(keyword)){
            String keywordstr = URLEncoder.encode(keyword);
            url = url + "&keyword=" + keywordstr + "&key=" + key;
            RequestParams params = new RequestParams(url);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    try {
                        data.clear();
                        JSONObject obj = new JSONObject(result);
                        JSONObject datas = obj.getJSONObject("datas");
                        String array = datas.getString("goods_list");
                        data.addAll(GoodsList.newInstanceList(array));
                       /* adapter = new GoodsListViewAdapter(getActivity(),data);
                        ref_listview.setAdapter(adapter);*/
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

    }

    @Override
    public void onPullRefresh() {
        data.clear();
        loadingGoodsListData();
        ref_listview.comLetepefresh();
    }

    @Override
    public void onLoadingMore() {
        pageno++;
        loadingGoodsListData();
        ref_listview.comLetepefresh();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        GoodsList bean = data.get(position-1);

        if (bean != null) {
            Intent intent = new Intent(getActivity(), GoodsDetailsActivity.class);
            intent.putExtra("goods_id", bean.getGoods_id());
            startActivity(intent);
        }
    }
}
